#ifndef DATABASE_TEST_H
#define DATABASE_TEST_H

#include "include/gtest.h"
#include "../Tool/include/Timer.h"
#include "../Tool/include/MiniDatabase.h"
#include "../Tool/include/ILogger.h"
using namespace Tool;

TEST(Database, DatabaseTest)
{
    return;
    MiniDatabase db("test_database.json");

    std::vector<FieldDefinition> userFields = {
        FieldDefinition("id", FieldType::Int, true, false),                 // 主键，非空
        FieldDefinition("name", FieldType::String, false, false),           // 非空
        FieldDefinition("age", FieldType::Int, false, true),
        FieldDefinition("email", FieldType::String, false, true),
        FieldDefinition("is_active", FieldType::Bool, false, true, true)    // 默认值true
    };

    bool createSuccess = db.CreateTable("users", userFields);
    LOG_INFO("创建users表: %s", createSuccess ? "成功" : "失败");

    Table* usersTable = db.GetTable("users");
    if (!usersTable)
    {
        LOG_ERROR("获取users表失败");
        return;
    }

    int count = 10;
    std::vector<std::string> names = { "张三", "李四", "小红", "小明", "小花" };
    std::vector<int> ages = { 15, 18, 20, 25, 30 };
    std::vector<std::string> emails = { "zhangsan@example.com", "lisi@example.com", "xiaohong@example.com", "xiaoming@example.com", "xiaohua@example.com" };

    int size = ages.size();
    Timer timer;
    for (int i = 1; i <= count; ++i)
    {
        int idx = (i - 1) % size;
        std::string sql = "insert into users set id=" + std::to_string(100000 + i) +
            ", name='" + names[idx] +
            "', age=" + std::to_string(ages[idx]) +
            ", email='" + emails[idx] + "'";
        db.Execute(sql);
    }
    LOG_INFO("插入了:%d 条记录 耗时：%.3lf", count, timer.Elapsed());

    timer.Reset();
    auto allUsers1 = db.Execute("select * from users");
    LOG_INFO("查询所有记录:%d/%d 条记录 耗时：%.3lf", allUsers1.m_result.size(), count, timer.Elapsed());

    timer.Reset();
    auto selects = db.Execute("select name, age from users where age > 18");
    LOG_INFO("查询年龄大于28的记录:%d/%d 条记录 耗时：%.3lf", selects.m_result.size(), count, timer.Elapsed());

    timer.Reset();
    auto selects1 = db.Execute("delete from users");
    LOG_INFO("删除的记录:%d/%d 条记录 耗时：%.3lf", selects1.m_result.size(), count, timer.Elapsed());

    timer.Reset();
    for (int i = 1; i <= count; ++i)
    {
        int idx = (i - 1) % size;
        std::string sql = "insert into users set id=" + std::to_string(100000 + i) +
            ", name='" + names[idx] +
            "', age=" + std::to_string(ages[idx]) +
            ", email='" + emails[idx] + "'";
        db.Execute(sql);
    }
    LOG_INFO("插入了:%d 条记录 耗时：%.3lf", count, timer.Elapsed());

    timer.Reset();
    auto selects2 = db.Execute("delete from users where id=”100001“");
    LOG_INFO("删除的记录:%d/%d 条记录 耗时：%.3lf", selects2.m_result.size(), count, timer.Elapsed());

    timer.Reset();
    auto allUsers3 = db.Execute("select * from users");
    LOG_INFO("查询所有记录:%d/%d 条记录 耗时：%.3lf", allUsers3.m_result.size(), count, timer.Elapsed());
}

TEST(Database, BasicOperations)
{
    // 创建数据库实例
    MiniDatabase db("test_database.db");
    LOG_INFO("创建数据库实例: test_database.db");

    // 定义用户表结构
    std::vector<FieldDefinition> userFields = {
        FieldDefinition("id", FieldType::Int, true, false),                 // 主键，非空
        FieldDefinition("name", FieldType::String, false, false),           // 非空
        FieldDefinition("age", FieldType::Int, false, true),
        FieldDefinition("email", FieldType::String, false, true),
        FieldDefinition("is_active", FieldType::Bool, false, true, true)    // 默认值true
    };

    bool createSuccess = db.CreateTable("users", userFields);
    LOG_INFO("创建users表: %s", createSuccess ? "成功" : "失败");

    Table* usersTable = db.GetTable("users");
    if (usersTable)
    {
        LOG_INFO("获取users表成功");
    }
    else
    {
        LOG_ERROR("获取users表失败");
        return;
    }

    int count = 100;
    std::vector<std::string> names = { "张三", "李四", "小红", "小明", "小花" };
    std::vector<int> ages = { 15, 18, 20, 25, 30 };
    std::vector<std::string> emails = { "zhangsan@example.com", "lisi@example.com", "xiaohong@example.com", "xiaoming@example.com", "xiaohua@example.com" };

    int size = ages.size();
    Timer timer;
    for (int i = 1; i <= count; ++i)
    {
        int idx = (i - 1) % size;
        std::string sql = "INSERT INTO users SET id=" + std::to_string(100000 + i) +
            ", name='" + names[idx] +
            "', age=" + std::to_string(ages[idx]) +
            ", email='" + emails[idx] + "'";
        db.Execute(sql);
    }
    LOG_INFO("插入了:%d 条记录 耗时：%.3lf", count, timer.Elapsed());

    timer.Reset();
    auto allUsers1 = db.Execute("select * from users");
    LOG_INFO("查询所有记录:%d/%d 条记录 耗时：%.3lf", allUsers1.m_result.size(), count, timer.Elapsed());

    timer.Reset();
    auto allUsers2 = db.Execute("SELECT name, age FROM users WHERE age > 18");
    LOG_INFO("查询年龄大于28的记录:%d/%d 条记录 耗时：%.3lf", allUsers2.m_result.size(), count, timer.Elapsed());

    timer.Reset();
    db.Execute("delete from users");
    LOG_INFO("删除所以的记录%d 条记录 耗时：%.3lf", count, timer.Elapsed());

    timer.Reset();
    for (int i = 1; i <= count; ++i)
    {
        int index = count % size;
        VariantHash user;
        user["id"] = 100000 + i;
        user["name"] = names[index];
        user["age"] = ages[index];
        user["email"] = emails[index];
        usersTable->Insert(std::move(user));
    }
    LOG_INFO("插入了:%d 条记录 耗时：%.3lf", count, timer.Elapsed());

    //timer.Reset();
    //auto allUsers = usersTable->Query();
    //LOG_INFO("查询所有记录:%d/%d 条记录 耗时：%.3lf", allUsers.size(), count, timer.Elapsed());

    //timer.Reset();
    //auto condition = ConditionBuilder::GreaterThan("age", 28);
    //auto adultUsers = usersTable->Query({ "id", "name", "age" }, condition);
    //LOG_INFO("查询年龄大于28的记录:%d/%d 条记录 耗时：%.3lf", adultUsers.size(), count, timer.Elapsed());

    //bool saveSuccess = db.Save();
}

#endif // DATABASE_TEST_H
    